home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevpe.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  7.5 KB  |  367 lines

  1. /* Copyright (C) 1989, 1990, 1991, 1994, 1996 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevpe.c,v 1.2 2000/09/19 19:00:19 lpd Exp $*/
  20. /*
  21.  * Private Eye display driver
  22.  *
  23.  * Hacked by Fran Taylor, Reflection Technology Inc.
  24.  */
  25.  
  26. #include "memory_.h"
  27. #include "gx.h"
  28. #include "gxdevice.h"
  29.  
  30. char *getenv(char *name);
  31.  
  32. typedef struct gx_device_pe_s {
  33.     gx_device_common;
  34.     byte *fbaddr;
  35.     unsigned regs;
  36. } gx_device_pe;
  37. #define pedev ((gx_device_pe *)dev)
  38.  
  39. typedef struct {
  40.     ushort reg, val;
  41. } regval;
  42.  
  43. #define XSIZE 720
  44. #define YSIZE 280
  45. #define BPL 90
  46. #define XPPI 160.0
  47. #define YPPI 96.0
  48. #define DEFAULT_ADDRESS ((byte *) 0xb8000000)
  49. #define DEFAULT_REGISTERS 0x3d0
  50.  
  51. dev_proc_open_device(pe_open);
  52. dev_proc_close_device(pe_close);
  53. dev_proc_fill_rectangle(pe_fill_rectangle);
  54. dev_proc_copy_mono(pe_copy_mono);
  55.  
  56. private gx_device_procs pe_procs =
  57. {    pe_open,
  58.     NULL,            /* get_initial_matrix */
  59.     NULL,            /* sync_output */
  60.     NULL,            /* output_page */
  61.     pe_close,
  62.     NULL,            /* map_rgb_color */
  63.     NULL,            /* map_color_rgb */
  64.     pe_fill_rectangle,
  65.     NULL,            /* tile_rectangle */
  66.     pe_copy_mono,
  67.     NULL            /* copy_color */
  68. };
  69.  
  70. gx_device_pe far_data gs_pe_device = 
  71. {    std_device_std_body(gx_device_pe, &pe_procs, "pe",
  72.       XSIZE, YSIZE, XPPI, YPPI),
  73.      { 0 },        /* std_procs */
  74.     DEFAULT_ADDRESS, DEFAULT_REGISTERS
  75. };
  76.  
  77. static regval peinit[] = {{0x04, 0x1e}, {0x05, 0x00},
  78.               {0x04, 0x0c}, {0x05, 0x21},
  79.               {0x04, 0x0d}, {0x05, 0x98},
  80.               {0x08, 0x00}, {0x08, 0x1e},
  81.               {0x04, 0x1e}, {0x05, 0x01}};
  82.  
  83. static regval pedone[] = {{0x04, 0x1e}, {0x05, 0x10},
  84.               {0x04, 0x0a}, {0x05, 0x00},
  85.               {0x04, 0x0b}, {0x05, 0x07},
  86.               {0x04, 0x0c}, {0x05, 0x00},
  87.               {0x04, 0x0d}, {0x05, 0x00},
  88.               {0x04, 0x0e}, {0x05, 0x00},
  89.               {0x04, 0x0f}, {0x05, 0x00},
  90.               {0x08, 0x00}, {0x08, 0x29}};
  91.  
  92. int pe_open(gx_device *dev)
  93. {
  94.     char *str;
  95.     int i;
  96.  
  97.     if ((str = getenv("PEFBADDR")) != 0)
  98.     {
  99.         if (!sscanf(str, "%lx", &(pedev->fbaddr)))
  100.         {
  101.             eprintf("Private Eye: PEFBADDR environment string format error\n");
  102.             exit(1);
  103.         }
  104.     }
  105.  
  106.     if ((str = getenv("PEREGS")) != 0)
  107.     {
  108.         if (!sscanf(str, "%x", &(pedev->regs)))
  109.         {
  110.             eprintf("Private Eye: PEREGS environment string format error\n");
  111.             exit(1);
  112.         }
  113.     }
  114.  
  115.     for (i = 0; i < 10; i++)
  116.         outportb(pedev->regs + peinit[i].reg, peinit[i].val);
  117.  
  118.     return 0;
  119. }
  120.  
  121. int pe_close(gx_device *dev)
  122. {
  123.     int i;
  124.  
  125.     /* restore the screen */
  126.     for (i = 0; i < 16; i++)
  127.         outportb(pedev->regs + pedone[i].reg, pedone[i].val);
  128.  
  129.     /* clear the frame buffer */
  130.     memset(pedev->fbaddr, 0, 4000);
  131.  
  132.     return 0;
  133. }
  134.  
  135. int pe_fill_rectangle(gx_device *dev, int x1, int y1, int w, int h,
  136.                       gx_color_index color)
  137. {
  138.     int x2, y2, xlen;
  139.     byte led, red, d;
  140.     byte *ptr;
  141.  
  142.     /* cull */
  143.  
  144.     if ((w <= 0) || (h <= 0) || (x1 > XSIZE) || (y1 > YSIZE))
  145.         return 0;
  146.  
  147.     x2 = x1 + w - 1;
  148.     y2 = y1 + h - 1;
  149.  
  150.     /* cull some more */
  151.  
  152.     if ((x2 < 0) || (y2 < 0))
  153.         return 0;
  154.  
  155.     /* clip */
  156.  
  157.     if (x1 < 0) x1 = 0;
  158.     if (x2 > XSIZE-1) x2 = XSIZE-1;
  159.     if (y1 < 0) y1 = 0;
  160.     if (y2 > YSIZE-1) y2 = YSIZE-1;
  161.  
  162.     w = x2 - x1 + 1;
  163.     h = y2 - y1 + 1;
  164.     xlen = (x2 >> 3) - (x1 >> 3) - 1;
  165.     led = 0xff >> (x1 & 7);
  166.     red = 0xff << (7 - (x2 & 7));
  167.  
  168.     ptr = pedev->fbaddr + (y1 * BPL) + (x1 >> 3);
  169.  
  170.     if (color)
  171.     {
  172.         /* here to set pixels */
  173.         
  174.         if (xlen == -1)
  175.         {
  176.             /* special for rectangles that fit in a byte */
  177.             
  178.             d = led & red;
  179.             for(; h >= 0; h--, ptr += BPL)
  180.                 *ptr |= d;
  181.             return 0;
  182.         }
  183.         
  184.         /* normal fill */
  185.         
  186.         for(; h >= 0; h--, ptr += BPL)
  187.         {    register int x = xlen;
  188.             register byte *p = ptr;
  189.             *p++ |= led;
  190.             while ( x-- ) *p++ = 0xff;
  191.             *p |= red;
  192.         }
  193.     }
  194.  
  195.     /* here to clear pixels */
  196.  
  197.     led = ~led;
  198.     red = ~red;
  199.  
  200.     if (xlen == -1)
  201.     {
  202.         /* special for rectangles that fit in a byte */
  203.         
  204.         d = led | red;
  205.         for(; h >= 0; h--, ptr += BPL)
  206.             *ptr &= d;
  207.         return 0;
  208.     }
  209.  
  210.     /* normal fill */
  211.         
  212.     for(; h >= 0; h--, ptr += BPL)
  213.     {    register int x = xlen;
  214.         register byte *p = ptr;
  215.         *p++ &= led;
  216.         while ( x-- ) *p++ = 0x00;
  217.         *p &= red;
  218.     }
  219.     return 0;
  220. }
  221.  
  222. int pe_copy_mono(gx_device *dev,
  223.          const byte *base, int sourcex, int raster, gx_bitmap_id id,
  224.                  int x, int y, int w, int h, 
  225.          gx_color_index zero, gx_color_index one)
  226. {
  227.     const byte *line;
  228.     int sleft, dleft;
  229.     int mask, rmask;
  230.     int invert, zmask, omask;
  231.     byte *dest;
  232.     int offset;
  233.  
  234. #define izero (int)zero
  235. #define ione (int)one
  236.  
  237. if ( ione == izero )        /* vacuous case */
  238.         return pe_fill_rectangle(dev, x, y, w, h, zero);
  239.  
  240.     /* clip */
  241.  
  242.     if ((x > XSIZE) || (y > YSIZE) || ((x + w) < 0) || ((y + h) < 0))
  243.         return 0;
  244.  
  245.     offset = x >> 3;
  246.     dest = pedev->fbaddr + (y * BPL) + offset;
  247.     line = base + (sourcex >> 3);
  248.     sleft = 8 - (sourcex & 7);
  249.     dleft = 8 - (x & 7);
  250.     mask = 0xff >> (8 - dleft);
  251.     if ( w < dleft )
  252.         mask -= mask >> w;
  253.     else
  254.         rmask = 0xff00 >> ((w - dleft) & 7);
  255.  
  256.     /* Macros for writing partial bytes. */
  257.     /* bits has already been inverted by xor'ing with invert. */
  258.  
  259. #define write_byte_masked(ptr, bits, mask)\
  260.   *ptr = ((bits | ~mask | zmask) & *ptr | (bits & mask & omask))
  261.  
  262. #define write_byte(ptr, bits)\
  263.   *ptr = ((bits | zmask) & *ptr | (bits & omask))
  264.  
  265. /*    if ( dev->invert )
  266.     {
  267.         if ( izero != (int)gx_no_color_index ) zero ^= 1;
  268.         if ( ione != (int)gx_no_color_index ) one ^= 1;
  269.     } */
  270.     invert = (izero == 1 || ione == 0 ? -1 : 0);
  271.     zmask = (izero == 0 || ione == 0 ? 0 : -1);
  272.     omask = (izero == 1 || ione == 1 ? -1 : 0);
  273.  
  274. #undef izero
  275. #undef ione
  276.  
  277.     if (sleft == dleft)        /* optimize the aligned case */
  278.     {
  279.         w -= dleft;
  280.         while ( --h >= 0 )
  281.         {
  282.             register const byte *bptr = line;
  283.             int count = w;
  284.             register byte *optr = dest;
  285.             register int bits = *bptr ^ invert;    /* first partial byte */
  286.             
  287.             write_byte_masked(optr, bits, mask);
  288.             
  289.             /* Do full bytes. */
  290.             
  291.             while ((count -= 8) >= 0)
  292.             {
  293.                 bits = *++bptr ^ invert;
  294.                 ++optr;
  295.                 write_byte(optr, bits);
  296.             }
  297.             
  298.             /* Do last byte */
  299.             
  300.             if (count > -8)
  301.             {
  302.                 bits = *++bptr ^ invert;
  303.                 ++optr;
  304.                 write_byte_masked(optr, bits, rmask);
  305.             }
  306.             dest += BPL;
  307.             line += raster;
  308.         }
  309.     }
  310.     else
  311.     {
  312.         int skew = (sleft - dleft) & 7;
  313.         int cskew = 8 - skew;
  314.         
  315.         while (--h >= 0)
  316.         {
  317.             const byte *bptr = line;
  318.             int count = w;
  319.             byte *optr = dest;
  320.             register int bits;
  321.             
  322.             /* Do the first partial byte */
  323.             
  324.             if (sleft >= dleft)
  325.             {
  326.                 bits = *bptr >> skew;
  327.             }    
  328.             else /* ( sleft < dleft ) */
  329.             {
  330.                 bits = *bptr++ << cskew;
  331.                 if (count > sleft)
  332.                     bits += *bptr >> skew;
  333.             }
  334.             bits ^= invert;
  335.             write_byte_masked(optr, bits, mask);
  336.             count -= dleft;
  337.             optr++;
  338.             
  339.             /* Do full bytes. */
  340.             
  341.             while ( count >= 8 )
  342.             {
  343.                 bits = *bptr++ << cskew;
  344.                 bits += *bptr >> skew;
  345.                 bits ^= invert;
  346.                 write_byte(optr, bits);
  347.                 count -= 8;
  348.                 optr++;
  349.             }
  350.             
  351.             /* Do last byte */
  352.             
  353.             if (count > 0)
  354.             {
  355.                 bits = *bptr++ << cskew;
  356.                  if (count > skew)
  357.                     bits += *bptr >> skew;
  358.                 bits ^= invert;
  359.                 write_byte_masked(optr, bits, rmask);
  360.             }
  361.             dest += BPL;
  362.             line += raster;
  363.         }
  364.     }
  365.     return 0;
  366. }
  367.